﻿using System.ComponentModel;
using System.Windows.Forms;
using AMatysiak.Methods;
using System;

namespace AMatysiak.Controls
{
    [DefaultProperty("TextFormat")]
    public class FormatLabel : Label
    {
        private string format;
        [Category("Appearance")]
        [DisplayName("TextFormat")]
        public string Format
        {
            get
            {
                if (format.IsEmpty())
                {
                    if (Text.IsEmpty())
                        return string.Empty;
                    else
                        format = Text;
                }

                return format;
            }
            set
            {
                format = value;
                actualizeText();
            }
        }

        private void actualizeText()
        {
            try
            {
                Text = string.Format(Format, args);

                if(this.InvokeRequired)
                {
                    Action invokeMethod = refreshControl;
                    this.Parent.BeginInvoke(invokeMethod);
                }
                else
                    refreshControl();
            }
            catch
            { Text = Format; }
        }

        private void refreshControl()
        {
            this.Refresh();
        }

        private object[] args;

        public void SetText(params object[] args)
        {
            this.args = args;
            actualizeText();
        }

        [Browsable(false)]
        public override string Text
        {
            get;
            set;
        }
    }
}
